﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CommonFunc;
using DAL = AccessDAL;

namespace BLL
{
    public class ProductClass
    {
        DAL.ProductClassTableAdapters.ProductClassTableAdapter DPC =
            new DAL.ProductClassTableAdapters.ProductClassTableAdapter();
        DAL.ProductTableAdapters.ProductTableAdapter DP =
            new DAL.ProductTableAdapters.ProductTableAdapter();

        MODEL.ProductClass _MPC = new MODEL.ProductClass();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MPC"></param>
        /// <returns></returns>
        public MODEL.ProductClass GetDataByID(MODEL.ProductClass MPC)
        {
            try
            {
                DataTable DT = DPC.GetDataByID(MPC.PC_ID);

                _MPC.PC_ClassCode = DT.Rows[0]["PC_ClassCode"].ToString();
                _MPC.PC_ID = Convert.ToInt32(DT.Rows[0]["PC_ID"]);
                _MPC.PC_IsLock = Convert.ToBoolean(DT.Rows[0]["PC_IsLock"]);
                _MPC.PC_Level = Convert.ToInt32(DT.Rows[0]["PC_Level"]);
                _MPC.PC_ParentID = Convert.ToInt32(DT.Rows[0]["PC_ParentID"]);
                _MPC.PC_Thumb = DT.Rows[0]["PC_Thumb"].ToString();
                _MPC.PC_Title = DT.Rows[0]["PC_Title"].ToString();

                return _MPC;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加类别
        /// <summary>
        /// 添加类别
        /// </summary>
        public void Add(MODEL.ProductClass MPC)
        {
            try
            {
                int Level;
                string classcode;

                DataTable DT_GetDataByTitle_ParentID = DPC.GetDataByTitle_ParentID(
                    MPC.PC_Title, MPC.PC_ID);

                if (DT_GetDataByTitle_ParentID.Rows.Count != 0)
                {
                    throw new Exception("此分类下，此类别名称已被占用");
                }

                DataTable DT_GetDataByParentID = DPC.GetDataByParentID(MPC.PC_ID);
                
                if (DT_GetDataByParentID.Rows.Count == 0)
                {
                    _MPC = GetDataByID(MPC);
                    Level = _MPC.PC_Level + 1;
                    classcode = ClassCode.CreateChildClassCode(_MPC.PC_ClassCode);
                }
                else
                {
                    string OldClassCode = DT_GetDataByParentID.Rows[0]["PC_ClassCode"].ToString();
                    Level = Convert.ToInt32(DT_GetDataByParentID.Rows[0]["PC_Level"]);
                    classcode = ClassCode.CreateNextClassCode(OldClassCode, Level.ToString());
                }

                DPC.Add(MPC.PC_Title, MPC.PC_ID, Level, classcode, MPC.PC_Thumb, MPC.PC_IsLock);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改类别
        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="MPC"></param>
        public void Mod(MODEL.ProductClass MPC)
        {
            try
            {
                DPC.Mod(MPC.PC_Title, MPC.PC_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        #endregion


        #region 删除类别
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="MPC"></param>
        public void Del(MODEL.ProductClass MPC)
        {
            try
            {
                DataTable DT;

                DT = DPC.GetDataByParentID(MPC.PC_ID);
                if (DT.Rows.Count != 0)
                {
                    throw new Exception("此类别下有子分类，请清空后重试");
                }

                DT = DP.GetDataByClassID(MPC.PC_ID);
                if (DT.Rows.Count != 0)
                {
                    throw new Exception("此类别下有产品或者回收站内有属于此类别产品，请清空后重试");
                }

                DT = DPC.GetDataByParentID(0);
                _MPC = GetDataByID(MPC);
                if (_MPC.PC_Level == 1 && DT.Rows.Count == 1)
                {
                    throw new Exception("根分类不得为零");
                }

                DPC.Del(MPC.PC_ID);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
